﻿CREATE PROCEDURE [dbo].[proc_Customer_CallOnTongJi_CheckInOutView]
	(
		@DepartmentId int,
		@ResultFlag int,
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@StartIndex int,
		@EndIndex int
	)
AS
Begin
	;WITH list As (Select  ROW_NUMBER() OVER (ORDER BY CheckOutTime DESC)AS Row,
		id,
		CompanyId,
		CustomerId,
		(select CustomerName from Customer where Customer.CustomerId=CheckInOut.CustomerId) as CustomerName,
		ContactPerson,
		ContactTel, 
		Address,
		MainProjectId,
		(Select Title From OProject Where Id=CheckInOut.MainProjectId) As MainProject,
		Content,
		Aim,
		Accompany,
		(Select Top 1 EName From [Employee] Where [Employee].UserName=CheckInOut.UserName) As UserName,
		ISNULL(CheckInTime,'') As CheckInTime,
		CheckOutTime,
		ResultFlag
	From CheckInOut 
	Where DepartmentId=@DepartmentId and ResultFlag=@ResultFlag and Convert(nchar(10),CheckOutTime,120)>=@StartDate and Convert(nchar(10),CheckOutTime,120)<=@EndDate
	)
	--Select * From list Where Row between @StartIndex and @EndIndex
	Select *,(Select Count(0) From list) As RecordCount From list Where Row between @StartIndex and @EndIndex Order By Row
End	
RETURN
